/**
 * Created by caoziwen on 2018/1/11.
 */
import axios from 'axios'

export const ERROK = 200
import qs from 'qs'

var api = 'api/'
process.env.NODE_ENV !== 'production' ? api = 'api/' : api = ''
/**获取采购一级分类*/
export function _getOneSelect() {
  const getOneSelect = axios.get(api + 'rest-iambuyer/purch/selectPList').then((res) => {
    return res.data
  }).catch(res => {
    return res.data
  })
  return getOneSelect
}
/**获取采购二级分类*/
export function _getTwoSelect(pid) {
  const getTwoSelect = axios.get(api + 'rest-iambuyer/purch/selectListByPid', 
  {
    params: {
      pid: pid
    }
  }).then((res) => {
    return res.data
  }).catch(res => {
    return res.data
  })
  return getTwoSelect
}
/*拿手机号获取城市经理接口*/
export function _usePhoneGetCityId(phone) {
  const usePhoneGetCityId = axios.get(api + 'rest-iambuyer/sms/pc/init?phone=' + phone).then((res) => {
    return res.data
  }).catch(res => {
    return res.data
  })
  return usePhoneGetCityId
}
/*登录页初始化接口*/
export function _initKorea(t) {
  const init = axios.get(api + 'rest-iambuyer/login/init?language=' + t).then((res) => {
    return res.data
  }).catch(res => {
    return res.data
  })
  return init
}

/*获取登录二维码*/
export function _getLoginErCode() {
  const getLoginErCode = axios.get(api + '/rest-sso/getLoginQr', {
    responseType: 'arraybuffer'
  }).then((res) => {
    return {
      code: 'data:image/png;base64,' + btoa(
        new Uint8Array(res.data)
        .reduce((data, byte) => data + String.fromCharCode(byte), '')
      ),
      r: res
    }
  }).catch(res => {
    return {
      code: 'data:image/png;base64,' + btoa(
        new Uint8Array(res.data)
        .reduce((data, byte) => data + String.fromCharCode(byte), '')
      ),
      r: res
    }
  })
  return getLoginErCode

}
/*轮询扫码登录结果*/
export function _vErCode(token) {
  const vErCode = axios.get(api + 'rest-sso/selectLoginState?token=' + token).then((res) => {
    return res
  }).catch(res => {
    return res
  })
  return vErCode

}

/*获取验证码接口*/
export function _getCode(id) {
  const getCode = axios.post(api + 'rest-iambuyer/sms/send?phone=' + id)
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  return getCode
}

/*短信验证码验证接口*/
export function _verifCode(phone, code) {
  const verifCode = axios.post(api + 'rest-iambuyer/sms/verif/pc?phone=' + phone + '&ticket=' + code)
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  return verifCode
}

/*发布采购页面资源初始化接口*/
export function _purch(phone, l) {
  const purch = axios.post(api + 'rest-iambuyer/purch/init?language=' + l + '&mobile=' + phone)
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  return purch
}

/*图片上传接口*/

// console.log(Vue.$vux)
export function _upLoadImg(formData) {
  const uploadimg = axios.post('http://img01.iambuyer.com/imgup/upLoad/fileUpload', formData).then((res) => {
    return res
  }).catch((res) => {
    return res
  })
  return uploadimg
}

// /*发布采购接口*/
export function _purchAdd(mobile, prodClass, prodClassCode, prodName, prodNumber, prodHighPrice, prodPrice, sampleProdNumer, intro, unit, imgs, cityUserId, paramString, prodid) {
  const purchAdd = axios.post(api + 'rest-iambuyer/purch/add', qs.stringify({
      mobile: mobile,
      prodClass: prodClass,
      prodClassCode: prodClassCode,
      prodName: prodName,
      prodNumber: prodNumber,
      prodHighPrice: prodHighPrice,
      prodPrice: prodPrice,
      sampleProdNumer: sampleProdNumer,
      intro: intro,
      unit: unit,
      imgs: imgs,
      cityUserId: cityUserId,
      paramString: JSON.stringify(paramString),
      prodid: prodid
    }))
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  return purchAdd
}

/*发布采购接口*/
// export function _purchAdd(mobile, prodClass, prodName, prodNumber, prodHighPrice, prodPrice,sampleProdNumer,  intro,unit,  prodImage1, prodImage2, prodImage3, cityUserId, paramString) {
//   const purchAdd = axios.post('http://10.158.41.132:9999/yunxin-rest-web-iambuyer/purch/add', qs.stringify({
//     mobile: mobile,
//     prodClass: prodClass,
//     prodName: prodName,
//     prodNumber: prodNumber,
//     prodHighPrice: prodHighPrice,
//     prodPrice: prodPrice,
//     sampleProdNumer: sampleProdNumer,
//     intro: intro,
//     unit: unit,
//     prodImage1: prodImage1,
//     prodImage2: prodImage2,
//     prodImage3: prodImage3,
//     cityUserId: cityUserId,
//     paramString: JSON.stringify(paramString)
//   }))
//   // {arrayFormat: 'repeat'}
//     .then(res => {
//       return res
//     }).catch(res => {
//       return res
//     })
//   return purchAdd
// }

/*发布采购成功初始化接口*/
export function _purResult(phone, l) {
  const result = axios.post(api + 'rest-iambuyer/purch/add/result?mobile=' + phone + '&language=' + l)
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  return result
}

/*求购列表页资源初始化接口*/
export function _initList(l) {
  const list = axios.post(api + 'rest-iambuyer/purch/list/init?language=' + l)
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  return list
}

/*用户采购数据列表接口*/
export function _showProlist(phone) {
  const _showProlist = axios.post(api + 'rest-iambuyer/purch/list?mobile=' + phone + '&state=0')
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  return _showProlist
}

/*详情页初始化接口*/
export function _initProDetail(l) {
  const initProDetail = axios.post(api + 'rest-iambuyer/purch/detail/init?language=' + l)
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  return initProDetail
}

/*采购详情数据接口*/
export function _proDetail(phone, id) {
  const proDetail = axios.post(api + 'rest-iambuyer/purch/detail?mobile=' + phone + '&prodId=' + id)
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  return proDetail
}

/*用户未登录提示并跳转*/
export function isLogin() {
  if (!getCookieVal('isLogin') || !getCookieVal('tel')) {
    const r = this.$router
    this.$vux.toast.show({
      type: 'warn',
      text: '로그인하십시오',
      onShow() {},
      onHide() {
        r.push({
          path: '/'
        })
      }
    })
  } else {
    return true
  }
}

/**
 * pc端发布采购页初始化城市经理列表
 * **/
export function _initPCPublish(l) {
  const _initPCPublish = axios.post(api + 'rest-iambuyer/purch/init?language=' + l)
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  return _initPCPublish
}

/**
 * pc端城市经理弹层初始化接口
 * **/
export function _initPCShowSelManage(l) {
  const _initPCShowSelManage = axios.post(api + 'rest-iambuyer/purch/cityusers/init?language=' + l)
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  return _initPCShowSelManage
}

/**
 * pc端查询某个省份下的城市接口
 * **/
export function _selCity(pcode) {
  const _selCity = axios.post(api + 'rest-iambuyer/purch/city?pcode=' + pcode)
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  return _selCity
}

/**
 * pc端查询某个省份下的城市经理接口
 * **/
export function _selProM(pcode) {
  const _selProM = axios.post(api + 'rest-iambuyer/purch/cityusers?pcode=' + pcode)
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  return _selProM
}

/**
 * pc端查询某个城市下的城市经理接口
 * **/
export function _selCityM(citycode) {
  const _selCityM = axios.post(api + 'rest-iambuyer/purch/cityusers?citycode=' + citycode)
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  return _selCityM
}

/**
 * pc端初始化添加参数
 * **/
export function _initAddPram(l) {
  const _initAddPram = axios.post(api + 'rest-iambuyer/purch/param/init?language=' + l)
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  return _initAddPram
}

/**
 * pc端初始化批量发布
 * **/
export function _initBatch(l) {
  const _initAddPram = axios.post(api + 'rest-iambuyer/purch/batch/init?language=' + l)
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  return _initAddPram
}

/**
 * pc批量发布接口
 * @tel:手机号
 * @formData:excel表格文件
 * @cityid:城市经理id
 * **/
export function _purchBatch(tel, cityid, formData) {
  const _purchBatch = axios.post(api + 'rest-iambuyer/batch/upload?phone=' + tel + '&cityid=' + cityid, formData)
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  return _purchBatch
}

/**
 *编辑采购信息数据获取接口
 * @prodid:当前要编辑的采购数据ID
 * @mobile:当前用户手机号
 * @language:语言类
 * **/
export function _editPurch(prodid, mobile, language) {
  const _editPurch = axios.post(api + 'rest-iambuyer/purch/detail/edit?prodid=' + prodid + '&mobile=' + mobile + '&language=' + language)
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  return _editPurch
}
/**
 *扫码登录查询用户类型
 * @mobile:当前用户手机号
 * **/
export function _pcInit(mobile) {
  const _pcInit = axios.post(api + 'rest-iambuyer/koreaUser/pcInit/' + mobile)
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  return _pcInit
}
